<template>
  <div class="page">
    <nav-com
      title="余额转让"
      :showView="true"
      @back="backHandler()"
    >
      <div
        @click="transferList()"
        style="font-size: 14px;color: #F86931;"
      >转让记录</div>
    </nav-com>
    <div style="padding: 20px;">
      <div style="line-height: 30px;display: flex;justify-content: space-between;">
        <div>手机号：{{userMobile}}</div>
        <!-- <div
          @click="transferList()"
          style="font-size: 14px;color: #F86931;margin-right:20px;margin-top:-6.5vh;float:right"
        >转让记录</div> -->
      </div>
    </div>
    <div
      class="padd_box"
      style="display: flex;margin:0 20px;align-items: center;justify-content: space-between;"
    >
      <van-col span="12">
        <div class="">
          <van-field
            label-class="label"
            :border="true"
            type="number"
            style="margin-top: 5vw"
            v-model="code"
            placeholder="请输入验证码"
          />
        </div>
      </van-col>
      <van-col
        span="10"
        style="text-align: right"
      >
        <div
          class="login_box"
          @click="codeHandler"
          v-if="codeFalg"
          style="text-align: center; color: #ff491f"
        >
          {{ codeText }}
        </div>
        <div
          class="login_box"
          v-else
          style="text-align: center; color: #ff491f"
        >
          {{ codeText }}
        </div>
      </van-col>
    </div>
    <div style="background-color: white; padding: 20px">
      <div>
        <span>对方手机号</span>
        <van-field
          label-class="label"
          :border="true"
          type="tel"
          style="margin-top: 5vw"
          v-model="mobile"
          @blur="transferUserHandler()"
          placeholder="请输入对方手机号"
        />
      </div>
      <div style="color: #818181; font-size: 14px; line-height: 40px;padding-left: 20px;">
        {{userName}}
      </div>
      <div>
        <span>转让金额</span>
        <van-field
          label-class="label"
          :border="true"
          type="number"
          style="margin-top: 5vw"
          v-model="score"
          placeholder="请输入转让金额"
        />
      </div>
      <div
        class="btn"
        @click="transfer"
      >提交</div>
    </div>
  </div>
</template>
    
    <script>
import navCom from "@/component/nav";
import { Toast, RadioGroup, Radio, Field } from "vant";
import {
  getFttuserUserId2,
  balance2Other,
  postVerificationcodes,
  score_send,
  verify_mobile,
} from "@/api/api";
export default {
  name: "index",
  components: {
    navCom,
  },
  data() {
    return {
      userMobile: "",
      mobile: "",
      score: "",
      radio: "1",
      codeText: "获取验证码",
      codeFalg: true,
      refDisabled: false,
      code: "",
      userName: null,
    };
  },

  mounted() {
    this.openType = this.$route.params["openType"];
    console.log(66, this.openType);
    getFttuserUserId2().then((res) => {
      console.log("getFttuserUserId2", res);
      if (res.data.code == 1) {
        let cellphone = res.data.data.mobile;
        this.userMobile = cellphone;
      }
    });
  },
  methods: {
    backHandler() {
      if (this.openType) {
        this.$router.go(-1);
      } else {
        if (weixin.isAndroid()) {
          window.JS_TROOPS.goBack();
        } else {
          this.$router.go(-1);
        }
      }
    },
    transferList() {
      this.$router.push({
        name: "balanceToOthersRecord",
        params: { type: 7, is_old: this.openType },
      });
    },
    transferUserHandler() {
      if (!this.mobile.trim()) {
        Toast("手机号不可以为空");
      } else {
        verify_mobile({
          mobile: this.mobile,
        }).then((res) => {
          console.log(res);
          if (res.data.code == 1) {
            this.userName = res.data.data.name;
          } else {
            this.$dialog.alert({
              message: res.data.msg,
            });
          }
        });
      }
    },
    async transfer() {
      console.log(this.$route.params.phone);
      let lastNum
      if(this.score){
        lastNum = this.score.split('.')[1]
      }
      if (!this.mobile.trim()) {
        Toast("手机号不可以为空");
      } else if (!this.score.trim()) {
        Toast("转让未输入");
      } else if (this.score<0.01) {
        Toast("转让金额需要大于0.01");
      } else if (lastNum && lastNum.length>2) {
        Toast("转让金额小数点后最多保留两位");
      } else if (!this.code.trim()) {
        Toast("验证码未输入");
      } else {
        this.LOADING = true;
        const res = await balance2Other({
          mobile: this.mobile,
          money: this.score,
          captcha: this.code,
          is_old: this.openType,
        });
        this.LOADING = false;
        console.log("scoreSubgift", res);
        Toast(res.data.msg);
        if (res.data.code == 1) {
          this.backHandler();
        }
      }
    },
    //获取验证码
    codeHandler() {
      let that = this;
      let telLen = this.userMobile.length;
      if (telLen == 11) {
        let time = null;
        clearInterval(time);
        that.codeFalg = false;
        let t = 60;
        time = setInterval(function () {
          t--;
          that.codeText = t;
          if (t <= 0) {
            that.codeText = "获取验证码";
            that.codeFalg = true;
            clearInterval(time);
          }
        }, 1000);

        score_send({
          mobile: this.userMobile,
          //  mobile: "17109919660"
        }).then((res) => {
          if (res.data.code == 1) {
            Toast("验证码已发送");
          } else {
            this.$dialog.alert({
              message: res.data.msg,
            });
          }
        });
        return true;
      } else {
        this.message = "请输入正确手机号";
        return false;
      }
    },
  },
};
</script>
    
    <style scoped>
body {
  background-color: #ffffff !important;
}
.page {
  width: 100vw;
  height: 100vh;
  background-color: white;
}
.btn {
  width: 50.6vw;
  height: 13.3vw;
  background: #f86830;
  margin: 0 auto;
  font-size: 4.5vw;
  text-align: center;
  line-height: 13.3vw;
  margin-top: 10vw;
  font-family: PingFangSC-Medium, PingFang SC;
  /*font-weight: 500;*/
  color: #ffffff;
  border-radius: 50px;
}
.label {
  font-size: 4.5vw;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333333;
}
.van-cell {
  /* box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.14); */
  border: 1px solid rgba(0, 0, 0, 0.14);
  border-radius: 8px;
  margin-bottom: 15px;
}

.login_box {
  border: 1px solid #f86931;
  color: #f86931;
  font-size: 4.2vw;
  border-radius: 10px;
  line-height: 45px;
}
.login_box label {
  padding-left: 5.3vw;
}
.login_box input {
  height: 11vw;
  line-height: 11vw;
  width: 50vw;
  border: none;
}
</style>